package com.transaction.demo.base.biz.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;
import static com.transaction.demo.base.enums.ExceptionEnum.ROLE_INFO_EXIST_ERROR;
import static com.transaction.demo.base.enums.ExceptionEnum.ROLE_INFO_NOT_EXIST_ERROR;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.transaction.demo.base.biz.SysRoleBiz;
import com.transaction.demo.base.biz.SysRoleMenuBiz;
import com.transaction.demo.base.dao.SysRoleDao;
import com.transaction.demo.base.exception.BaseServiceException;
import com.transaction.demo.base.biz.SysMenuBiz;
import com.transaction.demo.base.entity.SysRoleBean;
import com.transaction.demo.base.utils.BaseConstant;
import com.transaction.demo.base.vo.req.SaveRoleMenuListReqVo;
import com.transaction.demo.base.vo.req.SysRoleAddReqVo;
import com.transaction.demo.base.vo.req.SysRoleListReqVo;
import com.transaction.demo.base.vo.req.SysRoleMenuAddReqVo;
import com.transaction.demo.base.vo.req.SysRoleMenuListReqVo;
import com.transaction.demo.base.vo.req.SysRoleReqVo;
import com.transaction.demo.base.vo.req.SysRoleUpdateReqVo;
import com.transaction.demo.base.vo.resp.RoleMenuRespVo;
import com.transaction.demo.base.vo.resp.SysMenuRespVo;
import com.transaction.demo.base.vo.resp.SysRoleRespVo;
import com.transaction.demo.common.util.ResultPage;

/**
 * 描述：角色表
 * 作者：马庆智
 * 日期：2023/03/28 17:07:14
 */
@Slf4j
@Service
public class SysRoleBizImpl implements SysRoleBiz {

	@Autowired
	private SysRoleDao sysRoleDao;

	@Autowired
	private SysRoleMenuBiz roleMenuBiz;

	@Autowired
	private SysMenuBiz menuBiz;

	/**
	 * 查询角色表
	 *
	 * @param sysRole 请求参数
	 * @return SysRoleResp
	 * @author 马庆智
	 * @date: 2023/03/28 17:07:14
	 */
	@Override
	public SysRoleRespVo querySysRole(SysRoleReqVo sysRole) {

		SysRoleBean query = sysRoleDao.querySysRole(SysRoleBean.builder().build());

		return SysRoleRespVo.builder().build();
	}

	/**
	 * 查询角色表列表
	 *
	 * @param sysRole 请求参数
	 * @return SysRoleResp
	 * @author 马庆智
	 * @date: 2023/03/28 17:07:14
	 */
	@Override
	public ResultPage<SysRoleRespVo> querySysRoleList(SysRoleListReqVo sysRole) {

		PageHelper.startPage(sysRole.getCurrent(), sysRole.getPageSize());
		List<SysRoleBean> query = sysRoleDao.querySysRoleList(
				SysRoleBean.builder().roleCode(sysRole.getRoleCode()).roleName(sysRole.getRoleName())
						.status(sysRole.getStatus()).build());
		PageInfo<SysRoleBean> pageInfo = new PageInfo<>(query);

		List<SysRoleRespVo> list = pageInfo.getList().stream().map(x -> {
			SysRoleRespVo resp = new SysRoleRespVo();
			BeanUtils.copyProperties(x, resp);
			return resp;
		}).collect(Collectors.toList());

		return new ResultPage<>(list, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal());

	}

	/**
	 * 添加角色表
	 *
	 * @param addReq 请求参数
	 * @return int
	 * @author 马庆智
	 * @date: 2023/03/28 17:07:14
	 */
	@Override
	public int saveSysRole(SysRoleAddReqVo addReq) {
		SysRoleBean sysRoleBean = new SysRoleBean();
		sysRoleBean.setRoleCode(addReq.getRoleCode());
		sysRoleBean.setRoleName(addReq.getRoleName());

		SysRoleBean role = sysRoleDao.querySysRole(sysRoleBean);
		if (role != null) {
			log.error("角色信息:{}已存在", addReq);
			throw new BaseServiceException(ROLE_INFO_EXIST_ERROR);
		}

		BeanUtils.copyProperties(addReq, sysRoleBean);
		sysRoleBean.setCreatedBy("koobe");
		sysRoleBean.setCreatedDate(new Date());
		return sysRoleDao.saveSysRole(sysRoleBean);
	}

	/**
	 * 删除角色表
	 *
	 * @param ids 请求参数
	 * @return int
	 * @author 马庆智
	 * @date: 2023/03/28 17:07:14
	 */
	@Override
	public int deleteSysRole(String ids) {
		return sysRoleDao.deleteSysRole(
				Arrays.stream(ids.split(",")).map(Integer::parseInt).collect(Collectors.toList()));
	}

	/**
	 * 更新角色表
	 *
	 * @param updateReq 请求参数
	 * @return int
	 * @author 马庆智
	 * @date: 2023/03/28 17:07:14
	 */
	@Override
	public int updateSysRole(SysRoleUpdateReqVo updateReq) {
		SysRoleBean sysRoleBean = new SysRoleBean();
		sysRoleBean.setRoleId(updateReq.getRoleId());

		SysRoleBean role = sysRoleDao.querySysRole(sysRoleBean);
		if (role == null) {
			log.error("角色信息:{}不存在", updateReq);
			throw new BaseServiceException(ROLE_INFO_NOT_EXIST_ERROR);
		}

		BeanUtils.copyProperties(updateReq, sysRoleBean);
		sysRoleBean.setUpdatedBy("koobe");
		sysRoleBean.setUpdatedDate(new Date());
		return sysRoleDao.updateSysRole(sysRoleBean);
	}

	/**
	 * 角色菜单
	 *
	 * @param roleId 角色id
	 * @return list
	 * @author 马庆智
	 * @date: 2023/3/30 9:29
	 */
	@Override
	public RoleMenuRespVo queryRoleMenuList(Integer roleId) {
		List<SysMenuRespVo> sysMenuResps = menuBiz.querySysMenuList();

		List<Integer> menuIds = sysMenuResps.stream().map(SysMenuRespVo::getMenuId).collect(Collectors.toList());

		if (!roleId.equals(BaseConstant.ADMIN_ROLE_ID)) {
			menuIds = roleMenuBiz.querySysRoleMenuList(SysRoleMenuListReqVo.builder().roleId(roleId).build());
		}
		return RoleMenuRespVo.builder().menuIds(menuIds).menuList(sysMenuResps).build();
	}

	/**
	 * 分配角色菜单
	 *
	 * @param record 请求参数
	 * @return SysUserResp
	 * @author 马庆智
	 * @date: 2023/3/30 9:18
	 */
	@Override
	public Integer saveRoleMenuList(SaveRoleMenuListReqVo record) {
		Integer roleId = record.getRoleId();
		List<Integer> menuIds = record.getMenuIds();

		roleMenuBiz.deleteSysRoleMenu(roleId);

		for (Integer menuId : menuIds) {
			roleMenuBiz.saveSysRoleMenu(SysRoleMenuAddReqVo.builder().roleId(roleId).menuId(menuId).build());
		}

		return menuIds.size();
	}
}